# select the best solutions from the contextualization
args = commandArgs(TRUE) 
file1 = args[1]
error = args[2] # which solutions to keep: 1=only the absolute best ones, 2=best+the ones with one additional error, etc.
cutoff = args[3]

# read 
B = read.csv(file1,sep=" ",header=FALSE,stringsAsFactors=FALSE,quote="")
B = B[!is.na(B[,3]),]	#discart rows with only a 0
B = B[B[,5] < 1000,]
B = B[B[,6] < 1000,]

## Convert each configuration into an adjacency matrix
I = NULL
m = min(B[,7]) # least number of mismatches obtained across both phenotypes
for(i in 1:error){
  e = m + i - 1
  I = c(I, which(B[,7]==e))
}
B = B[I,]
B = B[,-c(1:7)]
print(sprintf("Total solutions: %s", nrow(B)))
B = unique(B)
print(sprintf("Unique solutions: %s", nrow(B)))
B = t(B)

write.table(B,sprintf("all_contextualized_best_adjacency-cutoff%s.txt",cutoff),sep=" ",row.names=FALSE,quote=FALSE, col.names=FALSE)

